//
//  BIZHotTopicsHomeVC.m
//  biz
//
//  Created by zhangfuwei on 2017/7/6.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZHotTopicsHomeVC.h"
#import "ZJScrollPageView.h"
#import "BIZHotTopicsParentListVC.h"
//model
#import "BIZHotTopicChannelModel.h"


@interface BIZHotTopicsHomeVC ()<ZJScrollPageViewDelegate,ZJScrollPageViewChildVcDelegate>

@property (nonatomic, strong) ZJScrollPageView *scrollPageView;
/** 标题列表 */
@property (nonatomic, strong) NSMutableArray *titles;

@end

@implementation BIZHotTopicsHomeVC{
    BIZHotTopicChannelModel *_channelM;// ChannelList
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //必要的设置, 如果没有设置可能导致内容显示不正常
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self initSubViews];
    [self requestScrollViewTitlesList];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initSubViews {
    self.title = @"热点问题";
    [self.view addSubview:self.scrollPageView];
    
}

#pragma mark -- 网络请求

- (void)requestScrollViewTitlesList {
    [MBProgressHUD showActivityMessageInView:nil];
    [RequestTool POST:kHotTopicChanelListURL
           parameters:nil
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  DeLog(@"热点问题(频道标签)--%@",responseObject);
                  [MBProgressHUD hideHUD];
                  _channelM = [BIZHotTopicChannelModel yy_modelWithJSON:responseObject];
                  if (_channelM.code.intValue == 1) {
                      
                      [_channelM.data enumerateObjectsUsingBlock:^(BIZHotTopicChannelDataModel * _Nonnull dataM, NSUInteger idx, BOOL * _Nonnull stop) {
                          [self.titles addObject:dataM.name];
                      }];
                      [self.scrollPageView reloadWithNewTitles:self.titles.copy];
                      
                  }else if (_channelM.code.intValue == 2){
                      [MBProgressHUD showTipMessageInView:@"请登录"];
                      [BIZUserModel shareUser].login = NO;
                      [self presentViewController:[BIZLoginViewController viewControllerFromStoryboard] animated:YES completion:nil];
                      
                  }else {
                      [MBProgressHUD showErrorMessage:_channelM.des];
                      
                  }
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  [MBProgressHUD hideHUD];
                  [MBProgressHUD showErrorMessage:@"网络不给力,请检测网络" timer:2.5];
                  DeLog(@"error ---%@",error);
                  
              }
     ];
}

#pragma mark -- scrollViewPageView 数据源&& 代理方法

- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    
    if (!childVc) {
        childVc = [[BIZHotTopicsParentListVC alloc] initWithUUID:_channelM.data[index].uuid];
    }
    
    return childVc;
}

#pragma mark -- lazyload

- (ZJScrollPageView *)scrollPageView {
    if (!_scrollPageView) {
        
        ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
        //显示滚动条
        style.showLine = YES;
        // 颜色渐变
        style.gradualChangeTitleColor = YES;
        style.selectedTitleColor = [UIColor zfw_ColorWithHex:0x1777cb alpha:1.0];
        style.normalTitleColor = [UIColor zfw_ColorWithHex:0x333333 alpha:1.0];
        style.scrollLineColor = [UIColor zfw_ColorWithHex:0x1777cb alpha:1.0];
        style.scrollLineHeight = 1.5f;
        // 初始化
        _scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) segmentStyle:style titles:nil parentViewController:self delegate:self];
    }
    
    return _scrollPageView;
}

-(NSMutableArray *)titles {
    if (!_titles) {
        _titles = [NSMutableArray array];
    }
    return _titles;
}
@end
